/*
 * _join.java
 *
 * Created on 2 Март 2008 г., 0:27
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package funcs;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.jivesoftware.smack.util.StringUtils;
import xmppbot.Exec;
import xmppbot.JJConnect;
import xmppbot.Main;

/**
 *
 * @author lian
 */
public class _join implements func {
    
    /** Creates a new instance of _join */
    public _join() {
    }

    public String process(Exec exe) {
        String ansver = null;
        int na = exe.nextArg;
        int count = exe.SplitMsg.length;
        String psevdonim = null;
        String conference = null;
        String name = null;
        String server_psevdonim = null;
        String server_name = null;
        JJConnect server = Main.Connect;
        ResultSet rs = null;
        String[] serv = null;
        String srv_name = null;
        switch (count - na) {
            case 1:
                psevdonim = exe.SplitMsg[na];
                try {
                    rs = exe.MySQLDB.getStatement().executeQuery("SELECT * FROM conferences WHERE psevdonim = '" + exe.parseSymbols(psevdonim) + "';");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

                try {
                    if (rs.next()) {
                        name = rs.getString("login");
                        conference = rs.getString("conference");
                        int server_int = rs.getInt("server");
                        rs = exe.MySQLDB.getStatement().executeQuery("SELECT * FROM servers WHERE id = " + server_int + ";");
                        rs.next();
                        server_name = rs.getString("user") + "@" + rs.getString("server") + "/" + rs.getString("resource");
                        if (((JJConnect) Main.Connects.get(server_name)).join(name, conference)) {
                            ansver = "Connected";
                        } else {
                            ansver = "Error";
                        }
                    } else {
                        ansver = "Bad conference psevdonim";
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                break;
            case 2:
                conference = exe.SplitMsg[na];
                name = exe.SplitMsg[na + 1];
                psevdonim = StringUtils.parseName(conference);
                if (((JJConnect) Main.Connects.get(Main.Connectsnames.get(0))).join(name, conference)) {
                    ansver = "Connected";
                } else {
                    ansver = "Error";
                }
                srv_name = StringUtils.parseName((String) Main.Connectsnames.get(0));
                serv = null;
                serv[0] = exe.parseSymbols(StringUtils.parseName((String) Main.Connectsnames.get(0)));
                serv[1] = exe.parseSymbols(StringUtils.parseServer((String) Main.Connectsnames.get(0)));
                serv[2] = exe.parseSymbols(StringUtils.parseResource((String) Main.Connectsnames.get(0)));
                try {
                    exe.MySQLDB.getStatement().executeUpdate("INSERT INTO conferences VALUES (null,'" + name + "','" + conference + "',(SELECT id FROM servers WHERE user = '" + serv[0] + "' AND server = '" + serv[1] + "' AND resource = '" + serv[2] + "'),'" + psevdonim + "',1);");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                break;
            case 3:
                conference = exe.SplitMsg[na];
                name = exe.SplitMsg[na + 1];
                psevdonim = exe.SplitMsg[na + 2];
                if (((JJConnect) Main.Connects.get(Main.Connectsnames.get(0))).join(name, conference)) {
                    ansver = "Connected";
                } else {
                    ansver = "Error";
                }
                srv_name = StringUtils.parseName((String) Main.Connectsnames.get(0));
                serv = null;
                serv[0] = exe.parseSymbols(StringUtils.parseName((String) Main.Connectsnames.get(0)));
                serv[1] = exe.parseSymbols(StringUtils.parseServer((String) Main.Connectsnames.get(0)));
                serv[2] = exe.parseSymbols(StringUtils.parseResource((String) Main.Connectsnames.get(0)));
                try {
                    exe.MySQLDB.getStatement().executeUpdate("INSERT INTO conferences VALUES (null,'" + name + "','" + conference + "',(SELECT id FROM servers WHERE user = '" + serv[0] + "' AND server = '" + serv[1] + "' AND resource = '" + serv[2] + "'),'" + psevdonim + "',1);");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                break;
            case 4:
                conference = exe.SplitMsg[na];
                name = exe.SplitMsg[na + 1];
                psevdonim = exe.SplitMsg[na + 2];
                server_psevdonim = exe.SplitMsg[na + 3];
                try {
                    rs = exe.MySQLDB.getStatement().executeQuery("SELECT * FROM servers WHERE psevdonim = '" + exe.parseSymbols(server_psevdonim) + "';");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                try {
                    if (rs.next()) {
                        server_name = rs.getString("user") + "@" + rs.getString("server") + "/" + rs.getString("resource");
                    } else {
                        ansver = "Bad server psevdonim";
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                if (((JJConnect) Main.Connects.get(server_name)).join(name, conference)) {
                    ansver = "Connected";
                } else {
                    ansver = "Error";
                }
                serv = null;
                serv[0] = exe.parseSymbols(StringUtils.parseName(server_name));
                serv[1] = exe.parseSymbols(StringUtils.parseServer(server_name));
                serv[2] = exe.parseSymbols(StringUtils.parseResource(server_name));
                try {
                    exe.MySQLDB.getStatement().executeUpdate("INSERT INTO conferences VALUES (null,'" + name + "','" + conference + "',(SELECT id FROM servers WHERE user = '" + serv[0] + "' AND server = '" + serv[1] + "' AND resource = '" + serv[2] + "'),'" + psevdonim + "',1);");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                break;
        }
//        String conference = exe.SplitMsg[exe.nextArg];
//        String name = exe.SplitMsg[exe.nextArg + 1];
//        String psevdonim = exe.SplitMsg[exe.nextArg + 2];
//        String server_psevdonim = exe.SplitMsg[exe.nextArg + 3];
//        boolean flag = true;
//        if (server_psevdonim != null) {
//            ResultSet rs = null;
//            try {
//                rs = exe.MySQLDB.getStatement().executeQuery("SELECT server FROM servers WHERE psevdonim = '" + exe.parseSymbols(server_psevdonim) + "';");
//            } catch (SQLException ex) {
//                ex.printStackTrace();
//                flag = false;
//                try {
//                    rs.close();
//                } catch (SQLException exp) {
//                    ex.printStackTrace();
//                }
//            }
//            try {
//                if (flag && rs.next()) {
//                    server = (JJConnect) Main.Connects.get(rs.getString("server"));
//                }
//            } catch (SQLException ex) {
//                ex.printStackTrace();
//            }
//        }
//        flag = server.join(name, conference);
//        if (flag && psevdonim != null) {
//            ResultSet rs = exe.MySQLDB.getStatement().executeUpdate("INSERT INTO conferences VALUES (null,'" + exe.parseSymbols(name) + "','" + exe.parseSymbols(conference) + "'," +  + ");");
//        }
        return ansver;
    }

    public String processnotallowed(Exec exe) {
        return null;
    }
    
    
}
